package com.lhy.admin.web;

import com.lhy.admin.common.mvcplus.controller.SimpleCrudControllerSupport;
import com.lhy.admin.common.mvcplus.entity.ReqCriteria;
import com.lhy.admin.common.mvcplus.entity.RespPage;
import com.lhy.admin.common.mvcplus.entity.RespResult;
import com.lhy.admin.common.secure.authentication.Authentication;
import com.lhy.admin.common.secure.authorization.Logical;
import com.lhy.admin.common.secure.authorization.Permissions;
import com.lhy.admin.common.treeable.Treeable;
import com.lhy.admin.common.treeable.TreeableUtil;
import com.lhy.admin.model.FileMapping;
import com.lhy.admin.model.extend.FileMappingTree;
import com.lhy.admin.service.FileMappingService;
import com.lhy.admin.util.AuthUtil;
import com.lhy.admin.util.QiniuUtil;
import com.lhy.admin.util.SpringContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("filemapping")
public class FileMappingController extends SimpleCrudControllerSupport<FileMappingService, FileMapping, Integer> {

    @Autowired
    AuthUtil authUtil;

    @Autowired
    QiniuUtil.QiniuPropertes qiniuPropertes;

    /**
     * 部门树接口，需要department.view部门查询权限
     **/
    @Permissions(value = "filemapping.view", logical = Logical.AND)
    @RequestMapping(value = "tree", method = RequestMethod.GET)
    public RespResult<List<Treeable>> tree(@Validated @ModelAttribute FileMapping bean) {
        List<FileMapping> fileMappings = service.list(bean);
        return RespResult.ok(TreeableUtil.toTree(fileMappings, FileMappingTree.class));
    }

    /**
     * FileMappingController
     * <p/>
     * 前台调用上传文件完毕后的保存方法
     *
     * @author hyluan
     * @date 2018/7/30 15:25
     * Copyright (c) 2018 wisedu
     */
    @Override
    public RespResult create(FileMapping mapping) {
        service.createFileMapping(mapping);
        return RespResult.ok().setData(mapping);
    }

    @Override
    @Permissions("filemapping.view")
    public RespResult<RespPage<FileMapping>> searchCriteria(ReqCriteria criteriaReq) {
        return super.searchCriteria(criteriaReq);
    }

    @Override
    @Permissions("filemapping.del")
    public RespResult delete(Integer id) {
        service.deleteFileMapping(id);
        return RespResult.ok();
    }

    @Override
    @Permissions("filemapping.del")
    public RespResult deleteCond(FileMapping bean) {
        return super.deleteCond(bean);
    }

    @RequestMapping(value = "upload/token", method = RequestMethod.GET)
    public RespResult uploadToken() {
        return RespResult.ok().setData(service.uploadToken());
    }

    @Override
    @Permissions("filemapping.view")
    public RespResult<FileMapping> get(Integer id) {
        return super.get(id);
    }

    @Override
    @Permissions("filemapping.view")
    public RespResult validate(FileMapping bean) {
        return super.validate(bean);
    }

    @Permissions("filemapping.edit")
    @Override
    public RespResult update(FileMapping bean) {
        return super.update(bean);
    }

    /**
     * @param parent
     * @return java.lang.Object
     * @throws
     * @Title: parents
     * @Description: 获取指定ID文件的所有父文件(夹), 包含该文件(夹)本身
     * @date 2018/6/21 16:43
     */
    @RequestMapping(value = "parents/{parent}", method = RequestMethod.GET)
    @Authentication
    public List<Map<String, Object>> parents(@PathVariable("parent") Integer parent) {
        List<FileMapping> mappings = service.findParentsById(parent);
        return mappings.stream().map(m -> {
            Map<String, Object> map = new HashMap<>(2);
            map.put("name", m.getName());
            map.put("id", m.getId());
            return map;
        }).collect(Collectors.toList());
    }

    @RequestMapping(value = "qiniuConfig", method = RequestMethod.GET)
    @Permissions("filemapping.view")
    public RespResult<QiniuUtil.QiniuPropertes> qiniuConfig() {
        return RespResult.<QiniuUtil.QiniuPropertes>ok().setData(qiniuPropertes);
    }

    @Override
    @Permissions("filemapping.view")
    public RespResult<List<FileMapping>> queryCond(FileMapping bean) {
        return super.queryCond(bean.setFolder(0));
    }

    @RequestMapping(value = "getUrl/{key}", method = RequestMethod.GET)
    public RespResult<Object> getUrl(@PathVariable("key") String key) {
        return RespResult.ok().setData(SpringContextUtil.getBean(QiniuUtil.class).buildUrl(key));
    }
}
